package com.mxpio.erp.hr.controller;

import com.mxpio.erp.common.hr.entity.AttendanceMonth;
import com.mxpio.erp.common.hr.service.AttendanceMonthService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "AttendanceMonthController", description = "员工月度考勤汇总接口")
@RestController
@RequestMapping("/erp/hr/attendancemonth/")
public class AttendanceMonthController {
	
	@Autowired
	private AttendanceMonthService service;

	@GetMapping("list")
	@Operation(summary = "员工月度考勤汇总列表", description = "获取员工月度考勤汇总列表", method = "GET")
	public Result<List<AttendanceMonth>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<AttendanceMonth> tos = service.list(AttendanceMonth.class, criteria);
		return Result.OK(tos);
	}
	
	@GetMapping("page")
	@Operation(summary = "员工月度考勤汇总列表", description = "获取员工月度考勤汇总列表", method = "GET")
	public Result<Page<AttendanceMonth>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<AttendanceMonth> page = service.listPage(AttendanceMonth.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	
	@PostMapping("add")
	@Operation(summary = "新增员工月度考勤汇总", description = "新增员工月度考勤汇总", method = "POST")
	public Result<AttendanceMonth> add(@RequestBody AttendanceMonth calendar) {
		service.save(calendar);
		return Result.OK(calendar);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑员工月度考勤汇总", description = "编辑员工月度考勤汇总（全量）", method = "PUT")
	public Result<AttendanceMonth> edit(@RequestBody AttendanceMonth calendar) {
		service.update(calendar);
		return Result.OK(calendar);
	}
	

	@GetMapping("calculate/{yearMonth}")
	@Operation(summary = "测算员工月度考勤汇总", description = "测算员工月度考勤汇总", method = "GET")
	public Result<?> calculateSummary(@Parameter(description = "yearMonth，格式2025-01",  required = true) @PathVariable(name = "yearMonth", required = true)  String yearMonth) {
		return service.calculateSummary(yearMonth);
	}
}
